package com.PVZ.event;

import com.PVZ.engine.Stage;
import com.PVZ.model.Entity;
import com.PVZ.model.emuns.EventType;
import com.PVZ.model.interfaces.EventListener;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.weapon.Weapon;
import com.PVZ.model.zombie.Zombie;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.Setter;

/**
 * Created by @Author: kabuda on @Date: 2021/5/20 21:15
 */
@Setter
public class AttackEventListener implements EventListener {

    private Stage stage;

    public AttackEventListener(Stage stage) {
        this.stage = stage;
    }

    /**
     * 事件处理类 所有事件在此处理
     *
     * @param event 事件
     */
    @Override
    public void eventHandler(Event event) {
        if (event.getEventType() == EventType.ATTACK) {
            AttackEvent attackEvent = (AttackEvent) event;

            Entity attacker = attackEvent.getAttacker();
            if (attacker instanceof Plant) {
                //需要攻击的植物生成子弹
                stage.getBulletFactory().generatePlantBullets((Plant) attacker, stage.getGettableFactory().getAllGettableList());

            } else if (attacker instanceof Zombie) {

                Weapon weapon = attacker.getWeapon();

                ((Zombie)attacker).startEat();

                //进行攻击
                weapon.attack(attacker, attackEvent.getEnemies());
            }
        }
    }
}
